package model;

import java.util.ArrayList;

// Representation of the 2D universe
import java.util.Vector;
public class Space2D {

	//Dimensions of the 2D space
	int dimX;
	int dimY;
	
	//Useful for giving an unique name to the ants
	int cptAnts;
	
	//Useful for giving an unique name to the resources
	int cptResources;
	
	//Map of the cell
	Cell[][] cellsMap;
	
	//the list of resources
	ArrayList<Resource> listRes;
	
	//the list of ants
	ArrayList<Ant> listAnts;
	
	//the list of cells with pheromones
	ArrayList<Cell> listCellsWithPheromone;
	
	boolean isPheromoneVanishable;
	
	public Space2D(int dimX_, int dimY_){
		dimX = dimX_;
		dimY = dimY_;
		cptAnts = 0;
		cptResources = 0;
		cellsMap = new Cell[dimX][dimY];
		initCellMap();
		listRes = new ArrayList<Resource>();
		listAnts = new ArrayList<Ant>();
		this.listCellsWithPheromone = new ArrayList<Cell>();
	}

	public int getDimX() {
		return dimX;
	}

	public int getDimY() {
		return dimY;
	}

	public int getCptAnts() {
		return cptAnts;
	}

	public void incCptAnts() {
		this.cptAnts ++;
	}

	public int getCptResources() {
		return cptResources;
	}

	public void incCptResources() {
		this.cptResources ++;
	}
	
	public void addAnt(Ant ant){
		Cell cell = this.cellsMap[ant.getLocation().getX()][ant.getLocation().getY()];
		cell.setAnt(ant);
		listAnts.add(ant);
	}
	
	public void addResource(Resource resource){
		Cell cell = this.cellsMap[resource.getLocation().getX()][resource.getLocation().getY()];
		cell.setResource(resource);
		listRes.add(resource);
	}
	
	public Cell getCell(Point location){
		return this.cellsMap[location.getX()][location.getY()];
	}
	
	public void setCell(Cell cell, Point location){
		this.cellsMap[location.getX()][location.getY()] = cell;
	}
	
	public Vector<Cell> getCellsAround (Point location){
		Vector<Cell> v = new Vector<Cell>();
		for (int i = location.getX()-1; i <= location.getX()+1; i++){
			if ((i>=0)&&(i<this.dimX)){
				for (int j = location.getY()-1; j <= location.getY()+1; j++){
					if ((j>=0)&&(j<this.dimY)){
						if ((i!=location.getX())||(j!=location.getY())){
							Cell cell = this.cellsMap[i][j];
							v.add(cell);
						}
					}
				}
			}
		}
		return v;
	}
	
	public void decPheromones(){
		if (this.isPheromoneVanishable){
		ArrayList<Cell> listCellsWithPheromone_ = new ArrayList<Cell>();
		for (int i = 0; i<this.listCellsWithPheromone.size(); i++){
			Cell cell = this.listCellsWithPheromone.get(i);
			cell.decPheromone();
			if (cell.getPheromone() != 0){
				listCellsWithPheromone_.add(cell);
			}
		}
		this.listCellsWithPheromone = listCellsWithPheromone_;
		}
	}
	
	public void setPheromoneActivated(boolean bool){
		if (!bool){
			for (int i = 0; i<this.getDimX();i++){
				for (int j =0; j<this.getDimY(); j++){
					Cell cell = this.cellsMap[i][j];
					if (cell != null){
						cell.removePheromone();
					}
				}
			}
		}
		for (int i = 0; i<this.listAnts.size();i++){
			this.listAnts.get(i).setPheromoneActivated(bool);
		}
	}
	
	/*
	public static void testGetCellsAround() throws Exception{
		Space2D space = new Space2D(50,50);
		Point p1 = new Point(10,10);
		Point p2 = new Point(11,10);
		Point p3 = new Point(12,10);
		
		Point p4 = new Point(33, 0);
		Point p5 = new Point(49, 49);
		
		Ant a1 = new Ant(space,p1);
		Ant a2 = new Ant(space,p1);
		Ant a3 = new Ant(space,p2);
		Ant a4 = new Ant(space,p2);
		Ant a5 = new Ant(space,p3);
		
		System.out.println("Around "+p2.toString());
		Vector<Cell> v = space.getCellsAround(p2);
		System.out.println("-> " + v.size() + "cells");
		for (int i = 0; i < v.size(); i++){
			Cell cell = v.get(i);
			if (cell != null){
				System.out.println(v.get(i).toString());
			}
		}
		
		System.out.println("Around "+p4.toString());
		v = space.getCellsAround(p4);
		System.out.println("-> " + v.size() + "cells");
		
		System.out.println("Around "+p5.toString());
		v = space.getCellsAround(p5);
		System.out.println("-> " + v.size() + "cells");

	}
	
	public static void main(String args[]) throws Exception{
		testGetCellsAround();
	}
	*/

	public ArrayList getListRes() {
		return listRes;
	}

	public ArrayList<Ant> getListAnts() {
		return listAnts;
	}
	
	public int getDimension() {
		return this.dimX;
	}
	
	public void clear() {
		this.cptAnts = 0;
		this.cptResources = 0;
		this.listAnts.clear();
		this.listRes.clear();
		this.listCellsWithPheromone.clear();
		this.cellsMap = new Cell[dimX][dimY];
		initCellMap();
	}

	public void setPheromoneVanishable(boolean pheromoneVanishable) {
		this.isPheromoneVanishable = pheromoneVanishable;
	}
	
	private void initCellMap() {
		for(int i = 0; i < dimX; i++) {
			for(int j = 0; j < dimY; j++) {
				this.cellsMap[i][j] = new Cell();
			}
		}
	}

	public Cell[][] getCellsMap() {
		return cellsMap;
	}

	public ArrayList<Cell> getListCellsWithPheromone() {
		return listCellsWithPheromone;
	}
}
